package com.baidu.interceptor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 创作时间：2019/12/17 9:25
 * 作者：李增强
 */
public class MyInterceptor implements HandlerInterceptor{

    @Value("aa")
    private String aa;


    private List<String> exceptUrls;

    public List<String> getExceptUrls() {
        return exceptUrls;
    }

    public void setExceptUrls(List<String> exceptUrls) {
        this.exceptUrls = exceptUrls;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        uri = uri.substring(uri.lastIndexOf("/")+1);


        /**
         * 把用户访问的url拿到，要是这个url在list里面，直接放行
         * list里面是不需要拦截的url，像登录的，验证码的
         */
        if(exceptUrls.contains(uri)){
            return true;
        }

        //
        Object ub = request.getSession().getAttribute("ub");
        System.out.println("这是我的拦截器开始工作了，其中ub====="+ub);
        if(ub!=null){
            return true;
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
